package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.domain.*;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.WipCompletionExport;
import com.pureut.storage.mapper.*;
import com.pureut.storage.service.*;
import com.pureut.storage.util.UnitUtil;
import com.pureut.storage.utils.DocumentTypeEnum;
import com.pureut.storage.utils.GenerationMethodEnum;
import com.pureut.storage.utils.VariousKeyEnum;
import com.pureut.storage.vo.*;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.common.redis.utils.RedisUtils;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.storage.api.dto.UpdateWipByAdjustmentVo;
import com.sale.storage.api.dto.WipCompletionMessageDto;
import com.sale.system.api.*;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.domain.dto.SysApiCategoryDto;
import com.sale.system.api.domain.dto.SysDocumentResultDto;
import com.sale.system.api.domain.dto.SysMaterialCodingResultDto;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/***
 * 完工入库实现层
 * Author:C
 * Date:2022/12/30 11:55
 * @DESC
 */
@Service
public class WipCompletionServiceImpl implements WipCompletionService {

    @Resource
    WipCompletionMapper wipCompletionMapper;

    @Resource
    PurchaseReceiptMapper purchaseReceiptMapper;

    @Resource
    WarehouseService warehouseService;

    @Resource
    WipCompletionDetailMapper wipCompletionDetailMapper;

    @Resource
    WmsStockInfoMapper wmsStockInfoMapper;

    @Resource
    PublicMaterialMapper publicMaterialMapper;

    @Resource
    WmsStockInfoService wmsStockInfoService;

    @Resource
    WipCompletionDetailService wipCompletionDetailService;

    @DubboReference
    RemoteUnitService remoteUnitService;

    @DubboReference
    RemoteCodeService remoteCodeService;

    @DubboReference
    RemoteMenuService remoteMenuService;

    @DubboReference
    RemoteDocumentService remoteDocumentService;

    @DubboReference
    RemoteCodingService remoteCodingService;

    @Resource
    IncomingMaterialPrintingDetailService incomingMaterialPrintingDetailService;

    @Resource
    IncomingMaterialPrintingMapper incomingMaterialPrintingMapper;

    @Resource
    UnitUtil unitUtil;


    /**
     * 完工入库列表
     *
     * @param wipCompletionVo
     * @return
     */
    @Override
    public TableDataInfo<WipCompletionDto> getReceiptList(WipCompletionVo wipCompletionVo, PageQuery pageQuery) throws Exception {
        Page<WipCompletionDto> wipCompletionList = wipCompletionMapper.getWipCompletionList(pageQuery.build(), wipCompletionVo);
        List<WipCompletionDto> records = wipCompletionList.getRecords();

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        //收发类型
        List<SysDictData> rdTypeArray = CacheUtils.get(CacheNames.SYS_DICT, "wip_completion_rd_type");
        Map<String, String> rdTypeMap = rdTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //获取完工入库状态字典值
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "wip_completion_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (WipCompletionDto entity : records) {
            //收发类型
            entity.setReceiveTypeDict(rdTypeMap.get(entity.getReceiveType()));
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //状态
            entity.setStatusDict(statusMap.get(entity.getStatus()));
            //仓库
            entity.setWarehousingWarehouse(entity.getWarehousingWarehouse());
            entity.setWarehousingWarehouseDict(entity.getWarehousingWarehouseDict());
            //单据类型
            entity.setDocumentTypeDict(DocumentTypeEnum.getValue(entity.getDocumentType()));
            //生成方式
            entity.setGenerationMethodDict(GenerationMethodEnum.getValue(entity.getGenerationMethod()));
            //物料类型
            entity.setMaterialName(entity.getMaterialName());
            entity.setMaterialCode(entity.getMaterialCode());
            entity.setMaterialSpec(entity.getMaterialSpec());

            if (entity.getMaterialUnit() != null) {
                //单位
                entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            }
//            if (entity.getMaterialMoreUnit() != null) {
//                //转换
//                UnitConversionDto unitConversionDto = unitUtil.atWillunitConversion(entity.getMaterialUnit(), entity.getMaterialCode(),
//                    String.valueOf(entity.getPlannedWeight()), entity.getMaterialId());
//
//                if (unitConversionDto != null) {
//                    entity.setUnitConversion(unitConversionDto.getMaterialMoreUnitNumber());
//                }
//            }
            //单位转换
            String s = unitUtil.unitC(entity.getMaterialCode(),
                String.valueOf(entity.getPlannedWeight()),
                entity.getMaterialId(),
                unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            entity.setUnitConversion(s);
        }
        return TableDataInfo.build(wipCompletionList);
    }

    /**
     * 新增
     *
     * @param wipCompletionVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addWipCompletion(WipCompletionVo wipCompletionVo) throws Exception {

        List<WipVo> wipList = wipCompletionVo.getWipList();
        List<WipCompletion> wipCompletionList = new ArrayList<>();
        if (wipList.size() > 0) {
            for (WipVo entity : wipList) {
                WipCompletion wipCompletion = new WipCompletion();
                String authorityCoding = remoteCodeService.authorityCoding("warehouse:warehousing:finished:list");
                if (authorityCoding == null) {
                    throw new GlobalException("该单据未进行配置，无法生成单据编码");
                }
                wipCompletion.setReceiptNo(remoteCodeService.splitList("WGRK"))
                    .setDeptId(LoginHelper.getDeptId())
                    .setStatus(1)
                    .setMaterialCode(entity.getMaterialCode())
                    .setMaterialUnit(entity.getMaterialUnit())
                    .setPlannedWeight(entity.getPlannedWeight())
                    .setWarehousingWarehouse(entity.getWarehousingWarehouse())
                    .setWarehousTime(wipCompletionVo.getWarehousTime())
                    .setRemark(wipCompletionVo.getRemark())
                    .setDrawerBy(LoginHelper.getUsername())
                    .setDrawerTime(new Date())
                    .setReceiveType(wipCompletionVo.getReceiveType())
                    .setCustomerCode(wipCompletionVo.getCustomerCode())
                    .setMakingOrderNo(entity.getMakingOrderNo())
                    .setCreateTime(new Date())
                    .setDocumentType(DocumentTypeEnum.WIP_COMPLETION.getCode());

//                if (wipCompletionVo.getGenerationMethod() != 2) {
                wipCompletion.setGenerationMethod(GenerationMethodEnum.MANUALLY_GENERATED.getCode());
//                } else {
//                    wipCompletion.setGenerationMethod(GenerationMethodEnum.AUTO_GENERATE.getCode());
//                }
                wipCompletionList.add(wipCompletion);
            }
            return wipCompletionMapper.insertBatch(wipCompletionList);
        } else {
            throw new GlobalException("请至少选择一条数据");
        }
    }

    /**
     * 关结
     *
     * @param wipCompletionVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(WipCompletionVo wipCompletionVo) {
        WipCompletion wipCompletion = wipCompletionMapper.selectById(wipCompletionVo.getId());
        //已完成、已关结不可重复关结；
        if (wipCompletion.getStatus() != 4 && wipCompletion.getStatus() != 3) {

            wipCompletion.setStatus(5)
                .setCloseReason(wipCompletionVo.getCloseReason())
                .setClosingBy(LoginHelper.getUsername())
                .setClosingTime(new Date());

            //库存中“未入库”的条码在关结后删除
            List<WmsStockInfo> wmsStockInfos = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiDocNum, wipCompletion.getReceiptNo()));
            if (wmsStockInfos.size() > 0) {
                //筛选出未入库的条码然后删除掉
                List<WmsStockInfo> collect = wmsStockInfos.stream().filter(t1 -> t1.getWsiMaterialStatus() == 1).collect(Collectors.toList());
                if (collect.size() > 0) {
                    wmsStockInfoMapper.deleteBatchIds(collect);
                }
            }

//            //待检验状态的单据关结后，单据状态更新为“已关结”，跳过后续操作，入库的物料显示为在库，关联的品质检验单状态更新为“已撤销”；
//            if (wipCompletion.getStatus() == 3) {
//                List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                //获取该单据下明细数据物料号码
//                List<String> materialNumberList = wipCompletionDetails.stream()
//                    .filter(entity -> entity.getStatus() == 2)
//                    .map(WipCompletionDetail::getMaterialNumber)
//                    .collect(Collectors.toList());
//                if (!materialNumberList.isEmpty()) {
//                    List<WmsStockInfo> wmsStockInfos = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().in(WmsStockInfo::getWsiMaterialNumber, materialNumberList));
//                    for (WmsStockInfo wmsStockInfo : wmsStockInfos) {
//                        wmsStockInfo.setWsiMaterialStatus(2);
//                    }
//                    wmsStockInfoMapper.updateBatchById(wmsStockInfos);
//                }
//
//            }
//            //待入库状态的单据关结后，单据状态更新为“已关结”，无影响；
////            if (wipCompletion.getStatus() == 1) {
////
////            }
//            //入库中的单据关结后，单据状态更新为“已关结”，已扫描过的条码为在库（完工入库未扫描到的条码在库存中没有库区库位）
//            if (wipCompletion.getStatus() == 2) {
//                List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                //获取该单据下明细数据物料号码
//                List<String> materialNumberList = wipCompletionDetails.stream()
//                    .filter(entity -> entity.getStatus() == 2 && entity.getStatus() == 3)
//                    .map(WipCompletionDetail::getMaterialNumber)
//                    .collect(Collectors.toList());
//                if (!materialNumberList.isEmpty()) {
//                    List<WmsStockInfo> wmsStockInfos = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().in(WmsStockInfo::getWsiMaterialNumber, materialNumberList));
//                    for (WmsStockInfo wmsStockInfo : wmsStockInfos) {
//                        wmsStockInfo.setWsiMaterialStatus(2);
//                    }
//                    wmsStockInfoMapper.updateBatchById(wmsStockInfos);
//                }
//            }
        } else {
            throw new GlobalException("已完成、已关结不可重复关结");
        }
        return wipCompletionMapper.updateById(wipCompletion) > 0;

    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public WipCompletionDto getView(Long id) {

        WipCompletionDto wipCompletionById = wipCompletionMapper.getWipCompletionById(id);

        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        //单据类型
        wipCompletionById.setDocumentTypeDict(DocumentTypeEnum.getValue(wipCompletionById.getDocumentType()));

        //生成方式
        wipCompletionById.setGenerationMethodDict(GenerationMethodEnum.getValue(wipCompletionById.getGenerationMethod()));

        //辅助属性
        List<SysAuxiliary> auxiliary = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + wipCompletionById.getTenantId(), String.valueOf(wipCompletionById.getMaterialId()));
        wipCompletionById.setAuxiliaryAttribute(auxiliary);
        //单位
        wipCompletionById.setMaterialUnit(wipCompletionById.getMaterialUnit());
        wipCompletionById.setUnitDict(unitByCodeToMap.get(wipCompletionById.getMaterialUnit()).getUnitName());


        List<WipVo> wipVoList = new ArrayList<>();
        WipVo wipVo = new WipVo();
        //计划量
        wipVo.setPlannedWeight(wipCompletionById.getPlannedWeight());
        //仓库
        wipVo.setWarehousingWarehouse(Long.parseLong(wipCompletionById.getWarehousingWarehouse()));
        wipVo.setWarehousingWarehouseDict(wipCompletionById.getWarehousingWarehouseDict());
        //物料信息
        wipVo.setMaterialName(wipCompletionById.getMaterialName());
        wipVo.setMaterialCode(wipCompletionById.getMaterialCode());
        wipVo.setMaterialSpec(wipCompletionById.getMaterialSpec());
        //单位
        wipVo.setMaterialUnit(wipCompletionById.getMaterialUnit());
        wipVo.setUnitDict(unitByCodeToMap.get(wipCompletionById.getMaterialUnit()).getUnitName());
        //辅助属性
        List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + wipCompletionById.getTenantId(), String.valueOf(wipCompletionById.getMaterialId()));
        wipVo.setAuxiliaryAttribute(statsArray);
        wipVo.setMakingOrderNo(wipCompletionById.getMakingOrderNo());

        wipVoList.add(wipVo);
        wipCompletionById.setWipList(wipVoList);
        return wipCompletionById;
    }


    /**
     * 修改
     *
     * @param wipCompletionVo
     * @return
     */
    @Override
    public boolean updateWipCompletion(WipCompletionVo wipCompletionVo) {

        WipCompletion wipCompletion = wipCompletionMapper.selectById(wipCompletionVo.getId());
        if (wipCompletion.getStatus() == 1) {

            wipCompletion.setWarehousTime(wipCompletionVo.getWarehousTime());
            wipCompletion.setDrawerTime(wipCompletionVo.getDrawerTime());
            wipCompletion.setRemark(wipCompletionVo.getRemark());
            List<WipVo> wipList = wipCompletionVo.getWipList();
            for (WipVo entity : wipList) {
                wipCompletion
                    .setMaterialUnit(entity.getMaterialUnit())
                    .setPlannedWeight(entity.getPlannedWeight())
                    .setWarehousingWarehouse(entity.getWarehousingWarehouse())
                    .setMaterialCode(entity.getMaterialCode());

            }
            return wipCompletionMapper.updateById(wipCompletion) > 0;
        } else {
            throw new GlobalException("只能修改待入库的数据");
        }
    }


    /**
     * 删除
     *
     * @param idStr
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWipCompletion(String idStr) {
        List<String> idList = Arrays.asList(idStr.split(","));
        if (idList.size() == 0) {
            throw new GlobalException("请至少选择一条数据");
        }
        List<WipCompletion> wipCompletionList = new ArrayList<>();
        List<WipCompletion> wipCompletions = wipCompletionMapper.selectList(new QueryWrapper<WipCompletion>().lambda().in(WipCompletion::getId, idList));
        for (WipCompletion entity : wipCompletions) {
            if (entity.getStatus() == 1) {
                wipCompletionList.add(entity);
            } else {
                throw new GlobalException("只能删除待入库的单据");
            }
        }
        List<WipCompletion> deleteList = new ArrayList<>();
        for (WipCompletion entity : wipCompletionList) {
            if (entity.getGenerationMethod() == 2) {
                deleteList.add(entity);
            } else {
                throw new GlobalException("自动生成的单据不能删除");
            }
        }
        return wipCompletionMapper.deleteBatchIds(deleteList) > 0;
    }


    /**
     * 导出
     *
     * @param wipCompletionVo
     * @return
     */
    @Override
    public List<WipCompletionExport> getInfoExport(WipCompletionVo wipCompletionVo) {
        List<WipCompletionExportDto> wipCompletionList;


        if (wipCompletionVo.getIdStr() == null) {
            wipCompletionList = wipCompletionMapper.getWipCompletionListOut(wipCompletionVo);
        } else {
            List<String> idList = Arrays.asList(wipCompletionVo.getIdStr().split(","));
            wipCompletionList = wipCompletionMapper.getDataByIdStr(idList);
        }

        //获取网型标准缓存字典数据
//        List<SysDictData> netArray = DictUtils.getDictCache("sys_of_type");
//        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//        //获取铺网类型标准缓存字典数据
//        List<SysDictData> netTypeArray = DictUtils.getDictCache("sys_type_net");
//        Map<String, String> netTypeMap = netTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//
//        //获取完工入库收发类型
//        List<SysDictData> rdArray = DictUtils.getDictCache("wip_completion_rd_type");
//        Map<String, String> rdMap = rdArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<WipCompletionExport> wipCompletionExportList = new ArrayList<>();
        for (WipCompletionExportDto entity : wipCompletionList) {
            WipCompletionExport wipCompletionExport = new WipCompletionExport();


            BeanUtils.copyProperties(entity, wipCompletionExport);

//            wipCompletionExport.setMaterialNetDict(netMap.get(entity.getMaterialNetDict()));
//            wipCompletionExport.setMaterialNetTypeDict(netTypeMap.get(entity.getMaterialNetTypeDict()));
            wipCompletionExportList.add(wipCompletionExport);
        }
        return wipCompletionExportList;
    }

//    /**
//     * 完工入库pda（自动生成）
//     *
//     * @param pickingRestDbVo
//     * @return
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public PickingRestDbDto wipCompletionPda(PickingRestDbVo pickingRestDbVo) throws Exception {
//        PickingRestDbDto pickingRestDbDto = new PickingRestDbDto();
//        WipCompletion wipCompletion = wipCompletionMapper.selectById(pickingRestDbVo.getId());
//        //判断该数据的生成方式
//        if (wipCompletion.getGenerationMethod() == 1) {
//            if (wipCompletion.getStatus() == 1 || wipCompletion.getStatus() == 2) {
//                //扫码入库
//                if ("1".equals(pickingRestDbVo.getType())) {
//                    //如果是第一步，需要查询库位信息
//                    if (pickingRestDbVo.getExecutionOrder() == 1) {
//                        WarehouseStringDto warehouseCode = warehouseService.getWarehouseCode(pickingRestDbVo.getInputValue());
//                        if (warehouseCode.getCombinationName() == null) {
//                            pickingRestDbDto.setMessage("该仓库不存在，请核实");
//                            pickingRestDbDto.setCode("500");
//                            return pickingRestDbDto;
//                        }
//                        pickingRestDbDto.setCombinationName(warehouseCode.getCombinationName());
//                        pickingRestDbDto.setCkId(warehouseCode.getCkId());
//                        pickingRestDbDto.setKqId(warehouseCode.getKqId());
//                        pickingRestDbDto.setKwId(warehouseCode.getKwId());
//                        pickingRestDbDto.setMessage("操作成功");
//                        pickingRestDbDto.setCode("200");
//                        return pickingRestDbDto;
//                    }
//                    //如果是第二步，需要根据物料号码查询明细信息
//                    if (pickingRestDbVo.getExecutionOrder() == 2) {
//                        WipCompletionDetail wipCompletionDetail = wipCompletionDetailMapper.selectOne(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getMaterialNumber, pickingRestDbVo.getInputValue()));
//                        //获取仓库数据
//                        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, wipCompletionDetail.getMaterialNumber()));
//                        //判断该明细是否已经入库过
//                        if (wipCompletionDetail.getStatus() == 2) {
//                            pickingRestDbDto.setMessage("该数据已经入库");
//                            pickingRestDbDto.setCode("500");
//                            return pickingRestDbDto;
//                        }
//                        PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, wipCompletionDetail.getMaterialCode()));
//                        //单位
//                        remoteUnitService.getUnitDataByCode()
//                        Long menuId = remoteMenuService.getIdByMenuName("完工入库");
//                        SysDocumentResultDto sysDocument = remoteDocumentService.getInfoConfig(menuId).getData();
//
//                        //否则就修改为已经完成
//                        wipCompletionDetail.setStatus(3);
//                        wipCompletionDetail.setWarehouseInBy(SecurityUtils.getUsername());
//                        wipCompletionDetail.setWarehouseInTime(new Date());
//                        wipCompletionDetailService.updateById(wipCompletionDetail);
//                        //同时修改仓库表中对应的数据状态为在库
//                        wmsStockInfo.setWsiMaterialStatus(2);
//                        wmsStockInfo.setWsiMaterialType(publicGoods.getMaterialType());
//                        wmsStockInfo.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
//                        wmsStockInfo.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
//                        wmsStockInfo.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
//                        wmsStockInfo.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
//                        wmsStockInfo.setWsiCategory(String.valueOf(publicGoods.getMaterialCategory()));
//                        wmsStockInfo.setWsiConnectNum(wipCompletion.getMakingOrderNo());
//                        wmsStockInfo.setWsiDocNum(wipCompletion.getReceiptNo());
//                        wmsStockInfo.setCreateUser(SecurityUtils.getUsername());
//                        wmsStockInfo.setCreateTime(new Date());
//                        wmsStockInfo.setWsiReveiveTime(new Date());
//                        wmsStockInfoService.updateById(wmsStockInfo);
//
//
//                        //获取单位字典值
//                        List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
//                        Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//
//                        //判断单位
//                        if ("2".equals(publicGoods.getMaterialUnit())) {
//                            //如果单位是千克
//                            if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
//                                wipCompletion.setActualQuantity(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                //修改采购入库信息入库重量
//                                wipCompletion.setWarehousingWeight(wipCompletionDetail.getNetWeight());
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                String s = bigDecimal.add(bigDecimal1).toString();
//                                wipCompletion.setActualQuantity(s);
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                String s1 = bigDecimal2.add(bigDecimal3).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//                            }
//                        } else if ("3".equals(publicGoods.getMaterialUnit())) {
//                            //如果单位是吨，若单位为吨，则实际量=入库重量（kg）/1000
//                            if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
//                                BigDecimal bigDecimal = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                BigDecimal divide = bigDecimal.divide(new BigDecimal("1000"));
//                                wipCompletion.setActualQuantity(divide.toString());
//                                //修改采购入库信息入库重量
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                String s1 = bigDecimal2.add(bigDecimal3).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                BigDecimal divide = bigDecimal1.divide(new BigDecimal("1000"));
//                                String s = bigDecimal.add(divide).toString();
//                                wipCompletion.setActualQuantity(s);
//                                //修改采购入库信息入库重量
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                String s1 = bigDecimal2.add(bigDecimal3).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//                            }
//                        } else {
//                            if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
//                                wipCompletion.setActualQuantity(String.valueOf(wipCompletionDetail.getReceiptQuantity()));
//                                //修改采购入库信息入库重量
//                                wipCompletion.setWarehousingWeight(wipCompletionDetail.getNetWeight());
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletionDetail.getReceiptQuantity()));
//                                String s = bigDecimal.add(bigDecimal1).toString();
//                                wipCompletion.setActualQuantity(s);
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                BigDecimal bigDecimal3 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
//                                String s1 = bigDecimal2.add(bigDecimal3).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//                            }
//                        }
//                        int warehousingNum = wipCompletion.getWarehousingNum();
//                        if (warehousingNum == 0) {
//                            wipCompletion.setWarehousingNum(1);
//                        } else {
//                            wipCompletion.setWarehousingNum(warehousingNum + 1);
//                        }
//
//                        pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + unitMap.get(publicGoods.getMaterialUnit()));
//                        pickingRestDbDto.setMessage("物料号码:" + wipCompletionDetail.getMaterialNumber() + "," + "净重:" + wipCompletionDetail.getNetWeight() + unitMap.get(publicGoods.getMaterialUnit()) + "," + "数量:" + wipCompletionDetail.getReceiptQuantity() + "," + "录入成功");
//                        pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
//                        pickingRestDbDto.setUnitDict(unitMap.get(publicGoods.getMaterialUnit()));
//                        pickingRestDbDto.setWarehouseNumber(String.valueOf(wipCompletion.getWarehousingNum()));
//
//                        //需求量
//                        double aDouble = Double.parseDouble(wipCompletion.getActualQuantity());
//                        //计划量
//                        double plannedWeight = wipCompletion.getPlannedWeight();
//                        boolean b = Double.doubleToLongBits(aDouble) >= Double.doubleToLongBits(plannedWeight);
//                        if (b) {
//                            //如果配置了送检
//                            if (sysDocument.getDocInspectionMark() == 1) {
//                                wipCompletion.setStatus(3);
//
//                                // 生成品质检验单
//                                if (sysDocument.getDocCode() == null) {
//                                    throw new GlobalException("未进行单据配置");
//                                }
//                                List<SysDocumentCheckDto> documentCheckDtoList = sysDocument.getDocumentCheckDtoList();
//                                if (documentCheckDtoList.size() < 1) {
//                                    throw new GlobalException("未进行检验单生成配置");
//                                }
//                                //通过物料编码获取物料类别
//                                //PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingSaleDetail.getItemCode()));
//                                //获取明细列表
//                                List<WipCompletionDetail> salesReturnDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                                StringBuilder stringBuilder = new StringBuilder();
//                                for (WipCompletionDetail entity : salesReturnDetailList) {
//                                    stringBuilder.append(entity.getBatchNo()).append(",");
//                                }
//                                StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
//
//                                for (SysDocumentCheckDto checkDto : documentCheckDtoList) {
//                                    if (checkDto.getCheckType() == publicGoods.getMaterialCategory()) {
//                                        QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
//                                        qualityInspectionDocumentVo.setItemRank(checkDto.getCheckSubmit());
//                                        qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
//                                        qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
//                                        qualityInspectionDocumentVo.setMaterialCode(wipCompletion.getMaterialCode());
//                                        qualityInspectionDocumentVo.setBatchNo(stringBuilder1.toString());
//                                        qualityInspectionDocumentVo.setAssociatedNo(wipCompletion.getReceiptNo());
//                                        qualityInspectionDocumentVo.setInspectDept(SecurityUtils.getDeptId());
//                                        qualityInspectionDocumentVo.setModeType(15);
//
//                                        List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNo = new ArrayList<>();
//                                        for (WipCompletionDetail entity : salesReturnDetailList) {
//                                            MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
//                                            materialNumberAndBatchNoVo.setBatchNo(entity.getBatchNo());
//                                            materialNumberAndBatchNoVo.setMaterialNumber(entity.getMaterialNumber());
//                                            materialNumberAndBatchNo.add(materialNumberAndBatchNoVo);
//                                        }
//                                        qualityInspectionDocumentVo.setMaterialNumberAndBatchNo(materialNumberAndBatchNo);
//
//                                        boolean b1 = qualityFeginService.addWares(qualityInspectionDocumentVo);
//                                        if (!b1) {
//                                            throw new GlobalException("未进行单据配置，请前往品质检验——>检验项目进行配置");
//                                        }
//                                    }
//                                }
//                                updateById(wipCompletion);
//                                wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                                pickingRestDbDto.setCode("200");
//                                return pickingRestDbDto;
//                            } else {
//                                //如果没有配置送检且单据对应的明细都已经入库，那么就修改为已完成
//                                boolean a = true;
//                                List<WipCompletionDetail> wipCompletionDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                                for (WipCompletionDetail entity : wipCompletionDetailList) {
//                                    if (entity.getStatus() != 3) {
//                                        wipCompletion.setStatus(2);
//                                        a = false;
//                                        break;
//                                    }
//                                }
//                                if (a) {
//                                    wipCompletion.setStatus(4);
//                                }
//                                wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                                updateById(wipCompletion);
//                                pickingRestDbDto.setCode("200");
//                                return pickingRestDbDto;
//                            }
//                        }
//                        wipCompletion.setStatus(2);
//                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                        updateById(wipCompletion);
//                        pickingRestDbDto.setCode("200");
//                        return pickingRestDbDto;
//                    }
//                }
//                //批次入库
//                if ("2".equals(pickingRestDbVo.getType())) {
//                    //如果是第一步，需要查询库位信息
//                    if (pickingRestDbVo.getExecutionOrder() == 1) {
//                        WarehouseStringDto warehouseCode = warehouseService.getWarehouseCode(pickingRestDbVo.getInputValue());
//                        pickingRestDbDto.setCombinationName(warehouseCode.getCombinationName());
//                        if (warehouseCode.getCombinationName() == null) {
//                            pickingRestDbDto.setMessage("该仓库不存在，请核实");
//                            pickingRestDbDto.setCode("500");
//                            return pickingRestDbDto;
//                        }
//                        pickingRestDbDto.setCkId(warehouseCode.getCkId());
//                        pickingRestDbDto.setKqId(warehouseCode.getKqId());
//                        pickingRestDbDto.setKwId(warehouseCode.getKwId());
//                        pickingRestDbDto.setMessage("操作成功");
//                        pickingRestDbDto.setCode("200");
//                        return pickingRestDbDto;
//                    }
//                    //如果是第二步，需要根据批次号查询明细信息
//                    if (pickingRestDbVo.getExecutionOrder() == 2) {
//                        List<WipCompletionDetail> wipCompletionDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getBatchNo, pickingRestDbVo.getInputValue()).eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                        //判断该批次中是否有已经入库的数据
//                        boolean isNoWarehousing = true;
//                        for (WipCompletionDetail entity : wipCompletionDetailList) {
//                            if (entity.getStatus() == 2) {
//                                isNoWarehousing = false;
//                                break;
//                            }
//                        }
//                        if (!isNoWarehousing) {
//                            pickingRestDbDto.setCode("500");
//                            pickingRestDbDto.setMessage("该批次中存在已经入库的数据，请核实后输入!");
//                            return pickingRestDbDto;
//                        }
//                        //净重总数
//                        BigDecimal bigDecimalNetWeight = new BigDecimal(0);
//                        //入库数量总数
//                        BigDecimal bigDecimalWarehouseNum = new BigDecimal(0);
//                        //循环明细信息
//                        for (WipCompletionDetail entity : wipCompletionDetailList) {
//                            bigDecimalNetWeight = bigDecimalNetWeight.add(new BigDecimal(String.valueOf(entity.getNetWeight())));
//                            bigDecimalWarehouseNum = bigDecimalWarehouseNum.add(new BigDecimal(String.valueOf(entity.getReceiptQuantity())));
//                        }
//
//                        PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, wipCompletion.getMaterialCode()));
//                        //获取单位字典值
//                        List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
//                        Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//                        //判断单位
//                        if ("2".equals(publicGoods.getMaterialUnit())) {
//                            //如果单位是千克
//                            if (wipCompletion.getActualQuantity() == null) {
//                                wipCompletion.setActualQuantity(bigDecimalNetWeight.toString());
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(bigDecimalNetWeight.toString()));
//                                wipCompletion.setWarehousingNum(Integer.parseInt(bigDecimalWarehouseNum.toString()));
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                String s = bigDecimal.add(bigDecimalNetWeight).toString();
//                                wipCompletion.setActualQuantity(s);
//
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                String s1 = bigDecimal1.add(bigDecimalNetWeight).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingNum()));
//                                String s2 = bigDecimal2.add(bigDecimalWarehouseNum).toString();
//                                wipCompletion.setWarehousingNum(Integer.parseInt(s2));
//                            }
//                        } else if ("3".equals(publicGoods.getMaterialUnit())) {
//                            //如果单位是吨，若单位为吨，则实际量=入库重量（kg）/1000
//                            if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
//                                BigDecimal bigDecimal = new BigDecimal(String.valueOf(bigDecimalNetWeight.toString()));
//                                BigDecimal divide = bigDecimal.divide(new BigDecimal("1000"));
//                                wipCompletion.setActualQuantity(divide.toString());
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                String s1 = bigDecimal1.add(bigDecimalNetWeight).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingNum()));
//                                String s2 = bigDecimal2.add(bigDecimalWarehouseNum).toString();
//                                wipCompletion.setWarehousingNum(Integer.parseInt(s2));
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(bigDecimalNetWeight.toString()));
//                                BigDecimal divide = bigDecimal1.divide(new BigDecimal("1000"));
//                                String s = bigDecimal.add(divide).toString();
//                                wipCompletion.setActualQuantity(s);
//                                //修改采购入库信息入库重量
//                                BigDecimal bigDecimal11 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                String s1 = bigDecimal11.add(bigDecimalNetWeight).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingNum()));
//                                String s2 = bigDecimal2.add(bigDecimalWarehouseNum).toString();
//                                wipCompletion.setWarehousingNum(Integer.parseInt(s2));
//                            }
//                        } else {
//                            if (wipCompletion.getActualQuantity() == null) {
//                                wipCompletion.setActualQuantity(bigDecimalWarehouseNum.toString());
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(bigDecimalNetWeight.toString()));
//                                wipCompletion.setWarehousingNum(Integer.parseInt(bigDecimalWarehouseNum.toString()));
//                            } else {
//                                BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
//                                String s = bigDecimal.add(bigDecimalWarehouseNum).toString();
//                                wipCompletion.setActualQuantity(s);
//
//                                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingWeight()));
//                                String s1 = bigDecimal1.add(bigDecimalNetWeight).toString();
//                                wipCompletion.setWarehousingWeight(Double.parseDouble(s1));
//
//                                BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(wipCompletion.getWarehousingNum()));
//                                String s2 = bigDecimal2.add(bigDecimalWarehouseNum).toString();
//                                wipCompletion.setWarehousingNum(Integer.parseInt(s2));
//                            }
//                        }
//
//
//                        pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + unitMap.get(publicGoods.getMaterialUnit()));
//                        pickingRestDbDto.setMessage("批次号:" + pickingRestDbVo.getInputValue() + "," + "净重:" + bigDecimalNetWeight + unitMap.get(publicGoods.getMaterialUnit()) + "," + "数量:" + bigDecimalWarehouseNum + "," + "录入成功");
//                        pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
//                        pickingRestDbDto.setUnitDict(unitMap.get(publicGoods.getMaterialUnit()));
//                        pickingRestDbDto.setWarehouseNumber(String.valueOf(bigDecimalWarehouseNum));
//
//
//                        //修改仓库表中对应的数据状态
//                        List<WmsStockInfo> wmsStockInfoList = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialBatch, pickingRestDbVo.getInputValue()));
//                        List<WmsStockInfo> wmsStockInfoArrayList = new ArrayList<>();
//
//                        Long menuId = feignService.getIdByMenuName("完工入库");
//                        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
//                        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
//
//                        //如果配置了送检,则校验是否配了检验方案
//                        if (sysDocument.getDocInspectionMark() == 1) {
//
//                            List<SysDocumentCheckDto> documentCheckDtoList = sysDocument.getDocumentCheckDtoList();
//                            if (documentCheckDtoList == null) {
//                                throw new GlobalException("请先在单据配置中配置送检方案");
//                            }
//
//
//                            QualitySchemeConfigurationVo qualitySchemeConfigurationVo = new QualitySchemeConfigurationVo();
//                            qualitySchemeConfigurationVo.setCategory(publicGoods.getMaterialCategory());
//                            qualitySchemeConfigurationVo.setItemRank(documentCheckDtoList.get(0).getCheckSubmit());
//                            qualitySchemeConfigurationVo.setMaterialCategory(publicGoods.getMaterialType());
//                            qualitySchemeConfigurationVo.setMaterialCode(wipCompletion.getMaterialCode());
//                            boolean check = qualityFeginService.check(qualitySchemeConfigurationVo);
//
//                            if (!check) {
//                                throw new GlobalException("该物料不存在对应的检验方案");
//
//                            }
//                        }
//
//                        //修改明细状态
//                        for (WipCompletionDetail entity : wipCompletionDetailList) {
//                            entity.setStatus(2);
//                        }
//                        wipCompletionDetailService.updateBatchById(wipCompletionDetailList);
////                        //并且校验是否还有待入库的明细
////                        List<WipCompletionDetail> collect = wipCompletionDetailList.stream().filter(entity -> entity.getStatus() == 1).collect(Collectors.toList());
//                        List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                        boolean allTrue = true;
//                        for (WipCompletionDetail entity : wipCompletionDetails) {
//                            if (entity.getStatus() == 1) {
//                                allTrue = false;
//                                break;
//                            }
//                        }
//                        //需求量
//                        double aDouble = Double.parseDouble(wipCompletion.getActualQuantity());
//                        //计划量
//                        double plannedWeight = wipCompletion.getPlannedWeight();
//                        boolean b = Double.doubleToLongBits(aDouble) >= Double.doubleToLongBits(plannedWeight);
//                        if (b) {
//
//                            //如果配置了送检
//                            if (sysDocument.getDocInspectionMark() == 1) {
//                                if (allTrue) {
//                                    wipCompletion.setStatus(3);
//                                } else {
//                                    wipCompletion.setStatus(2);
//                                }
//                                // 生成品质检验单
//                                if (sysDocument.getDocCode() == null) {
//                                    throw new GlobalException("未进行单据配置");
//                                }
//                                List<SysDocumentCheckDto> documentCheckDtoList = sysDocument.getDocumentCheckDtoList();
//                                if (documentCheckDtoList.size() < 1) {
//                                    throw new GlobalException("未进行检验单生成配置");
//                                }
//                                //通过物料编码获取物料类别
//                                //PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingSaleDetail.getItemCode()));
//                                //获取明细列表
//                                List<WipCompletionDetail> salesReturnDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                                StringBuilder stringBuilder = new StringBuilder();
//                                for (WipCompletionDetail entity : salesReturnDetailList) {
//                                    stringBuilder.append(entity.getBatchNo()).append(",");
//                                }
//                                StringBuilder stringBuilder1 = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
//
//                                for (SysDocumentCheckDto checkDto : documentCheckDtoList) {
//                                    if (checkDto.getCheckType() == publicGoods.getMaterialCategory()) {
//                                        QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
//                                        qualityInspectionDocumentVo.setItemRank(checkDto.getCheckSubmit());
//                                        qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
//                                        qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
//                                        qualityInspectionDocumentVo.setMaterialCode(wipCompletion.getMaterialCode());
//                                        qualityInspectionDocumentVo.setBatchNo(stringBuilder1.toString());
//                                        qualityInspectionDocumentVo.setAssociatedNo(wipCompletion.getReceiptNo());
//                                        qualityInspectionDocumentVo.setInspectDept(SecurityUtils.getDeptId());
//                                        qualityInspectionDocumentVo.setModeType(15);
//
//                                        List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNo = new ArrayList<>();
//                                        for (WipCompletionDetail entity : salesReturnDetailList) {
//                                            MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
//                                            materialNumberAndBatchNoVo.setBatchNo(entity.getBatchNo());
//                                            materialNumberAndBatchNoVo.setMaterialNumber(entity.getMaterialNumber());
//                                            materialNumberAndBatchNo.add(materialNumberAndBatchNoVo);
//                                        }
//                                        qualityInspectionDocumentVo.setMaterialNumberAndBatchNo(materialNumberAndBatchNo);
//
//
////                                            qualityFeginService.add(qualityInspectionDocumentVo);
//                                        boolean b1 = qualityFeginService.addWares(qualityInspectionDocumentVo);
//                                        if (!b1) {
//                                            throw new GlobalException("未进行单据配置，请前往品质检验——>检验项目进行配置");
//                                        }
////                                        AjaxResult add = qualityFeginService.add(qualityInspectionDocumentVo);
////                                        String code = add.get("code").toString();
////                                        if ("500".equals(code)) {
////                                            PickingRestDbDto pickingRestDbDto1 = new PickingRestDbDto();
////                                            pickingRestDbDto1.setMessage("生成品质检验单失败");
////                                            pickingRestDbDto1.setCode("500");
////                                            return pickingRestDbDto1;
////                                        }
//                                    }
//                                }
//
//                                //修改仓库数据
//                                for (WmsStockInfo entity : wmsStockInfoList) {
//                                    entity.setWsiMaterialStatus(2);
//                                    entity.setWsiMaterialType(publicGoods.getMaterialType());
//                                    entity.setWsiCategory(String.valueOf(publicGoods.getMaterialCategory()));
//                                    entity.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
//                                    entity.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
//                                    entity.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
//                                    entity.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
//                                    entity.setWsiConnectNum(wipCompletion.getMakingOrderNo());
//                                    entity.setWsiDocNum(wipCompletion.getReceiptNo());
//                                    entity.setCreateUser(SecurityUtils.getUsername());
//                                    entity.setCreateTime(new Date());
//                                    entity.setWsiReveiveTime(new Date());
//                                    wmsStockInfoArrayList.add(entity);
//                                }
//                                wmsStockInfoService.updateBatchById(wmsStockInfoArrayList);
////                                //修改明细状态
////                                for (WipCompletionDetail entity : wipCompletionDetailList) {
////                                    entity.setStatus(2);
////                                }
////                                wipCompletionDetailService.updateBatchById(wipCompletionDetailList);
////                                //如果该单据明细中都已经待检验，就把单据修改为待检验
////                                List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
////                                List<WipCompletionDetail> collect1 = wipCompletionDetails.stream().filter(entity -> entity.getStatus() != 2).collect(Collectors.toList());
////                                if (collect1.size() == 0) {
////                                    wipCompletion.setStatus(3);
////                                }
//                                wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                                updateById(wipCompletion);
//                                pickingRestDbDto.setCode("200");
//                                return pickingRestDbDto;
//                            } else {
//                                if (allTrue) {
//                                    wipCompletion.setStatus(4);
//                                } else {
//                                    wipCompletion.setStatus(2);
//                                }
//                                wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                                updateById(wipCompletion);
//
//                                //修改仓库数据
//                                for (WmsStockInfo entity : wmsStockInfoList) {
//                                    entity.setWsiMaterialStatus(2);
//                                    entity.setWsiMaterialType(publicGoods.getMaterialType());
//                                    entity.setWsiCategory(String.valueOf(publicGoods.getMaterialCategory()));
//                                    entity.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
//                                    entity.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
//                                    entity.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
//                                    entity.setWsiConnectNum(wipCompletion.getMakingOrderNo());
//                                    entity.setWsiDocNum(wipCompletion.getReceiptNo());
//                                    entity.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
//                                    entity.setWsiReveiveTime(new Date());
//                                    entity.setCreateUser(SecurityUtils.getUsername());
//                                    entity.setCreateTime(new Date());
//                                    wmsStockInfoArrayList.add(entity);
//                                }
//                                wmsStockInfoService.updateBatchById(wmsStockInfoArrayList);
////                                //修改明细状态
////                                for (WipCompletionDetail entity : wipCompletionDetailList) {
////                                    entity.setStatus(2);
////                                }
////                                wipCompletionDetailService.updateBatchById(wipCompletionDetailList);
////                                //如果该单据明细中都已经待检验，就把单据修改为待检验
////                                List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getBatchNo, pickingRestDbVo.getInputValue()).eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
////                                List<WipCompletionDetail> collect1 = wipCompletionDetails.stream().filter(entity -> entity.getStatus() != 2).collect(Collectors.toList());
////                                if (collect1.size() == 0) {
////                                    wipCompletion.setStatus(3);
////                                }
//                                wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                                updateById(wipCompletion);
//                                pickingRestDbDto.setCode("200");
//                                return pickingRestDbDto;
//                            }
//                        }
//                        //修改明细状态
////                        for (WipCompletionDetail entity : wipCompletionDetailList) {
////                            entity.setStatus(2);
//                        //修改仓库数据
//                        for (WmsStockInfo wmsStockInfo : wmsStockInfoList) {
//                            wmsStockInfo.setWsiMaterialStatus(2);
//                            wmsStockInfo.setWsiMaterialType(publicGoods.getMaterialType());
//                            wmsStockInfo.setWsiCategory(String.valueOf(publicGoods.getMaterialCategory()));
//                            wmsStockInfo.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
//                            wmsStockInfo.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
//                            wmsStockInfo.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
//                            wmsStockInfo.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
//                            wmsStockInfo.setWsiConnectNum(wipCompletion.getMakingOrderNo());
//                            wmsStockInfo.setWsiDocNum(wipCompletion.getReceiptNo());
//                            wmsStockInfo.setWsiReveiveTime(new Date());
//                            wmsStockInfo.setCreateUser(SecurityUtils.getUsername());
//                            wmsStockInfo.setCreateTime(new Date());
//                            wmsStockInfoArrayList.add(wmsStockInfo);
//                        }
//                        wmsStockInfoService.updateBatchById(wmsStockInfoArrayList);
////                        }
////                        wipCompletionDetailService.updateBatchById(wipCompletionDetailList);
//                        wipCompletion.setStatus(2);
//                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//                        updateById(wipCompletion);
//                        pickingRestDbDto.setCode("200");
//                        return pickingRestDbDto;
//                    }
//                }
//
//
//                //库位撤销
//                if ("3".equals(pickingRestDbVo.getType())) {
////                    //如果是第一步，需要查询库位信息
////                    if (pickingRestDbVo.getExecutionOrder() == 1) {
////                        WarehouseStringDto warehouseCode = warehouseService.getWarehouseCode(pickingRestDbVo.getInputValue());
////                        if (warehouseCode.getCombinationName() == null) {
////                            pickingRestDbDto.setMessage("该仓库不存在，请核实");
////                            pickingRestDbDto.setCode("500");
////                            return pickingRestDbDto;
////                        }
////                        pickingRestDbDto.setWarehouseMessage(warehouseCode.getCombinationName());
////                        pickingRestDbDto.setCkId(warehouseCode.getCkId());
////                        pickingRestDbDto.setKqId(warehouseCode.getKqId());
////                        pickingRestDbDto.setKwId(warehouseCode.getKwId());
////                        pickingRestDbDto.setMessage("操作成功");
////                        pickingRestDbDto.setCode("200");
////                        return pickingRestDbDto;
////                    }
//                    //如果是第二步，需要根据批次号查询明细信息
////                    if (pickingRestDbVo.getExecutionOrder() == 2) {
//                    WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDbVo.getInputValue()).eq(WmsStockInfo::getWsiMaterialStatus, 2));
//                    if (wmsStockInfo == null) {
//                        pickingRestDbDto.setMessage("撤销失败，请核实后再录入");
//                        pickingRestDbDto.setCode("500");
//                        return pickingRestDbDto;
//                    }
//                    wmsStockInfo.setWsiMaterialStatus(1);
//                    wmsStockInfo.setWsiWarehourse("");
//                    wmsStockInfo.setWsiReservoirArea("");
//                    wmsStockInfo.setWsiStorageLoaction("");
//                    wmsStockInfo.setWsiWarehourseInfo("");
//                    wmsStockInfoMapper.updateById(wmsStockInfo);
//
//                    PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, wipCompletion.getMaterialCode()));
//                    //获取单位字典值
//                    List<SysDictData> unitArray = DictUtils.getDictCache("sys_material_unit");
//                    Map<String, String> unitMap = unitArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
//
//                    //同时修改采购入库明细状态
//                    WipCompletionDetail wipCompletionDetail = wipCompletionDetailMapper.selectOne(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getMaterialNumber, pickingRestDbVo.getInputValue()));
//
//                    wipCompletionDetail.setStatus(1);
//                    wipCompletionDetailService.updateById(wipCompletionDetail);
//
//                    //完工入库单数量和重量扣减
//                    wipCompletion.setWarehousingNum(wipCompletion.getWarehousingNum() - 1);
//                    BigDecimal subtract = new BigDecimal(wipCompletion.getActualQuantity()).subtract(new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight())));
//                    wipCompletion.setWarehousingWeight(Double.parseDouble(subtract.toString()));
//                    wipCompletion.setActualQuantity(subtract.toString());
//
//
//                    //判断条数修改采购入库状态
//                    List<WipCompletionDetail> list = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, pickingRestDbVo.getId()));
//                    boolean isSuccess = true;
//                    for (WipCompletionDetail entity : list) {
//                        if (entity.getStatus() == 2 || entity.getStatus() == 3) {
//                            isSuccess = false;
//                            break;
//                        }
//                    }
//                    //假如全是待入库就修改完工入库为待入库
//                    if (isSuccess) {
//                        wipCompletion.setStatus(1);
//                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
//
//                    }
//                    updateById(wipCompletion);
//
//                    //设置返回信息
//                    pickingRestDbDto.setMessage("物料号码:" + wipCompletionDetail.getMaterialNumber() + "," + "净重:" + wipCompletionDetail.getNetWeight() + unitMap.get(publicGoods.getMaterialUnit()) + "," + "数量:" + wipCompletionDetail.getReceiptQuantity() + "," + "撤销成功");
//                    pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + unitMap.get(publicGoods.getMaterialUnit()));
//                    pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
//                    pickingRestDbDto.setUnitDict(unitMap.get(publicGoods.getMaterialUnit()));
//                    pickingRestDbDto.setWarehouseNumber(String.valueOf(wipCompletion.getWarehousingNum()));
//                    pickingRestDbDto.setCode("200");
//                    return pickingRestDbDto;
////                    }
//                }
//            } else {
//                pickingRestDbDto.setCode("500");
//                pickingRestDbDto.setMessage("只能操作待入库和入库中得数据");
//                return pickingRestDbDto;
//            }
//            return pickingRestDbDto;
//        } else {
//            pickingRestDbDto.setMessage("只能操作自动生成的数据");
//            pickingRestDbDto.setCode("500");
//            return pickingRestDbDto;
//        }
//    }

    /**
     * 完工入库pda(手动生成)
     *
     * @param pickingRestDbVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickingRestDbDto manualWipCompletionPda(PickingRestDbVo pickingRestDbVo) throws Exception {
        PickingRestDbDto pickingRestDbDto = new PickingRestDbDto();
        WipCompletion wipCompletion = wipCompletionMapper.selectById(pickingRestDbVo.getId());
        Long menuId = remoteMenuService.getIdByMenuName("完工入库");
        SysDocumentResultDto sysDocument = remoteDocumentService.getInfoConfig(menuId);
        //待入库和入库中的数据才能进行操作
        if (wipCompletion.getStatus() == 1 || wipCompletion.getStatus() == 2) {
            //扫码入库
            if ("1".equals(pickingRestDbVo.getType())) {
                //如果是第一步，需要查询库位信息
                if (pickingRestDbVo.getExecutionOrder() == 1) {
                    WarehouseStringDto warehouseCode = warehouseService.getWarehouseCode(pickingRestDbVo.getInputValue());
                    if (warehouseCode.getCombinationName() == null) {
                        pickingRestDbDto.setMessage("该仓库不存在，请核实");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }
                    pickingRestDbDto.setCombinationName(warehouseCode.getCombinationName());
                    pickingRestDbDto.setCkId(warehouseCode.getCkId());
                    pickingRestDbDto.setKqId(warehouseCode.getKqId());
                    pickingRestDbDto.setKwId(warehouseCode.getKwId());
                    pickingRestDbDto.setMessage("操作成功");
                    pickingRestDbDto.setCode("200");
                    return pickingRestDbDto;
                }
                //如果是第二步，需要根据物料号码保存明细信息
                if (pickingRestDbVo.getExecutionOrder() == 2) {

//                    //判断是否配置超收(如果未配置超收且实际量大于等于计划量就给提示)
//                    if (sysDocument.getDocExcessMark() == 2) {
//                        double aDouble;
//                        if (wipCompletion.getActualQuantity() == null) {
//                            aDouble = 0;
//                        } else {
//                            aDouble = Double.parseDouble(wipCompletion.getActualQuantity());
//                        }
//                        //每一次将实际量和计划量进行比较
//                        double plannedWeight = wipCompletion.getPlannedWeight();
//                        boolean b = (int) Math.round(aDouble) >= (int) Math.round(plannedWeight);
//                        if (b) {
//                            //修改单据为已完成
//                            wipCompletion.setStatus(4);
//                            wipCompletionMapper.updateById(wipCompletion);
//                            List<WipCompletionDetail> wipCompletionDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>()
//                                .lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//                            //修改来料打印状态为已完成
//                            IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialPrinting>()
//                                .lambda().eq(IncomingMaterialPrinting::getReceiptNo, wipCompletion.getReceiptNo())
//                                .eq(IncomingMaterialPrinting::getBatchNo, wipCompletionDetailList.get(0).getBatchNo()));
//                            //把对应的来料打印数据状态修改为已完成
//                            incomingMaterialPrinting.setPrintStatus(4);
//                            incomingMaterialPrintingMapper.updateById(incomingMaterialPrinting);
//                            pickingRestDbDto.setMessage("该单据未配置超收，且实际量大于等于计划量，无法继续入库，请核实");
//                            pickingRestDbDto.setCode("500");
//                            return pickingRestDbDto;
//                        }
//                    }

                    String materialNumber = pickingRestDbVo.getInputValue();
                    //获取完工入库明细信息
                    WipCompletionDetail wipCompletionDetail = wipCompletionDetailMapper.selectOne(new QueryWrapper<WipCompletionDetail>()
                        .lambda()
                        .eq(WipCompletionDetail::getMaterialNumber, materialNumber));

                    if (wipCompletionDetail == null) {
                        pickingRestDbDto.setMessage("没有查询到对应物料，请核实后输入!");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }
                    boolean a = wipCompletion.getId() == wipCompletionDetail.getWipCompletionId();

                    if (!a) {
                        pickingRestDbDto.setMessage("不是本单据对应的物料，请核实!");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }

                    if (wipCompletionDetail.getStatus() != 1) {
                        pickingRestDbDto.setMessage("该单据已经入库");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }

                    if (sysDocument == null) {
                        pickingRestDbDto.setMessage("没有查询到相关单据配置");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }
                    //获取物料类别信息
                    SysApiCategoryDto data = remoteCodingService.getByCode(wipCompletionDetail.getMaterialCode()).getData();
                    //单位
                    MaterialUnitDto data1 = remoteUnitService.getUnitDataByCode(Long.parseLong(wipCompletion.getMaterialUnit())).getData();
                    //修改明细状态为已入库
                    wipCompletionDetail.setStatus(3);
                    wipCompletionDetail.setWarehouseInTime(new Date());
                    wipCompletionDetail.setWarehouseInBy(LoginHelper.getUsername());
                    wipCompletionDetail.setWarehouseMessage(pickingRestDbVo.getCombinationName());
                    wipCompletionDetail.setReceiptQuantity(1L);
                    wipCompletionDetailMapper.updateById(wipCompletionDetail);
                    //修改对应仓库数据状态
                    WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, materialNumber));
                    wmsStockInfo.setWsiMaterialStatus(2);
                    wmsStockInfo.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
                    wmsStockInfo.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
                    wmsStockInfo.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
                    wmsStockInfo.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
                    wmsStockInfo.setWsiDocNum(wipCompletion.getReceiptNo());
                    wmsStockInfo.setWsiConnectNum(wipCompletion.getMakingOrderNo());
                    wmsStockInfo.setWsiCategory(String.valueOf(data.getCategoryId()));
                    wmsStockInfo.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                    wmsStockInfo.setWsiReveiveTime(new Date());
                    wmsStockInfo.setWsiMaterialGrade("1");
                    wmsStockInfo.setUnitPrice(new BigDecimal("0.00"));
                    wmsStockInfo.setWsiCustomCode(wipCompletion.getCustomerCode());
                    if (data1.getUnitQuantity() != null) {
                        wmsStockInfo.setWsiMinunitNum(Integer.parseInt(data1.getUnitQuantity()));
                    }
                    wmsStockInfoMapper.updateById(wmsStockInfo);


                    //修改完工入库实际量
                    if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
                        wipCompletion.setActualQuantity(String.valueOf(wipCompletionDetail.getNetWeight()));
                    } else {
                        BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
                        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight()));
                        String s = bigDecimal.add(bigDecimal1).toString();
                        wipCompletion.setActualQuantity(s);
                    }

                    if (wipCompletion.getWarehousingNum() == 0) {
                        wipCompletion.setWarehousingNum(1);
                    } else {
                        wipCompletion.setWarehousingNum(wipCompletion.getWarehousingNum() + 1);
                    }

                    pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + data1.getUnitName());
                    pickingRestDbDto.setMessage("物料号码:" + wipCompletionDetail.getMaterialNumber() + "," + "数量:" + wipCompletionDetail.getNetWeight() + "," + "录入成功");
//                        pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
                    pickingRestDbDto.setUnitDict(data1.getUnitName());
                    pickingRestDbDto.setWarehouseNumber(String.valueOf(wipCompletion.getWarehousingNum()));


                    double aDouble = Double.parseDouble(wipCompletion.getActualQuantity());
                    double planWeight = wipCompletion.getPlannedWeight();

                    boolean b = Double.doubleToLongBits(aDouble) >= Double.doubleToLongBits(planWeight);
                    //判断该明细下物料是否都已经入库
                    boolean isAll = true;
                    List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
                    for (WipCompletionDetail entity : wipCompletionDetails) {
                        if (entity.getStatus() != 3) {
                            isAll = false;
                            break;
                        }
                    }
                    //判断是否配置了超收
                    if (sysDocument.getDocExcessMark() == 2) {
                        //如果未配置超收，且实际量大于等于计划量，就将单据修改为已完成
                        if (b && isAll) {
                            wipCompletion.setStatus(4);
//                            if (sysDocument.getDocExcessMark() == 1) {
//                                wipCompletion.setStatus(2);
//                            } else {
//                                wipCompletion.setStatus(4);
//                            }
                        } else {
                            wipCompletion.setStatus(2);
                        }

//                        pickingRestDbDto.setCode("200");
//                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getCkId());
//                        wipCompletionMapper.updateById(wipCompletion);
//                        return pickingRestDbDto;
                    } else {
                        wipCompletion.setStatus(2);
                    }

                    pickingRestDbDto.setCode("200");
                    wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getCkId());
                    wipCompletionMapper.updateById(wipCompletion);
                    return pickingRestDbDto;
                }
            }

            //批次入库
            if ("2".equals(pickingRestDbVo.getType())) {
                //如果是第一步，需要查询库位信息
                if (pickingRestDbVo.getExecutionOrder() == 1) {
                    WarehouseStringDto warehouseCode = warehouseService.getWarehouseCode(pickingRestDbVo.getInputValue());
                    if (warehouseCode.getCombinationName() == null) {
                        pickingRestDbDto.setMessage("该仓库不存在，请核实");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }
                    pickingRestDbDto.setCombinationName(warehouseCode.getCombinationName());
                    pickingRestDbDto.setCkId(warehouseCode.getCkId());
                    pickingRestDbDto.setKqId(warehouseCode.getKqId());
                    pickingRestDbDto.setKwId(warehouseCode.getKwId());
                    pickingRestDbDto.setMessage("操作成功");
                    pickingRestDbDto.setCode("200");
                    return pickingRestDbDto;
                }
                //如果是第二步，需要根据批次号查询明细信息
                if (pickingRestDbVo.getExecutionOrder() == 2) {
                    List<WipCompletionDetail> wipCompletionDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>()
                        .lambda()
                        .eq(WipCompletionDetail::getBatchNo, pickingRestDbVo.getInputValue())
                        .eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId())
                        .eq(WipCompletionDetail::getStatus, 1));
                    if (wipCompletionDetailList.size() == 0) {
                        pickingRestDbDto.setMessage("未获取到对应明细数据，请核实");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }
//                    //判断该批次中是否有已经入库的数据
//                    boolean isNoWarehousing = true;
//                    for (WipCompletionDetail entity : wipCompletionDetailList) {
//                        if (entity.getStatus() == 2) {
//                            isNoWarehousing = false;
//                            break;
//                        }
//                    }
//                    if (!isNoWarehousing) {
//                        pickingRestDbDto.setCode("500");
//                        pickingRestDbDto.setMessage("该批次中存在已经入库的数据，请核实后输入!");
//                        return pickingRestDbDto;
//                    }
                    //净重总数
                    BigDecimal bigDecimalNetWeight = new BigDecimal(0);
                    //入库数量总数
                    BigDecimal bigDecimalWarehouseNum = new BigDecimal(0);

                    //循环明细信息
                    for (WipCompletionDetail entity : wipCompletionDetailList) {
                        bigDecimalNetWeight = bigDecimalNetWeight.add(new BigDecimal(String.valueOf(entity.getNetWeight())));
                        bigDecimalWarehouseNum = bigDecimalWarehouseNum.add(new BigDecimal(String.valueOf(entity.getReceiptQuantity())));
                    }

                    //单位
                    MaterialUnitDto data1 = remoteUnitService.getUnitDataByCode(Long.parseLong(wipCompletion.getMaterialUnit())).getData();

                    //修改完工入库实际量
                    if (wipCompletion.getActualQuantity() == null || "".equals(wipCompletion.getActualQuantity())) {
                        wipCompletion.setActualQuantity(String.valueOf(bigDecimalNetWeight));
                    } else {
                        BigDecimal bigDecimal = new BigDecimal(wipCompletion.getActualQuantity());
                        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(bigDecimalNetWeight));
                        String s = bigDecimal.add(bigDecimal1).toString();
                        wipCompletion.setActualQuantity(s);
                    }

                    if (wipCompletion.getWarehousingNum() == 0) {
                        wipCompletion.setWarehousingNum(wipCompletionDetailList.size());
                    } else {
                        wipCompletion.setWarehousingNum(wipCompletion.getWarehousingNum() + 1);
                    }


                    pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + data1.getUnitName());
                    pickingRestDbDto.setMessage("批次号:" + pickingRestDbVo.getInputValue() + "," + "数量:" + bigDecimalNetWeight + "," + "录入成功");
//                        pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
                    pickingRestDbDto.setUnitDict(data1.getUnitName());
                    pickingRestDbDto.setWarehouseNumber(String.valueOf(wipCompletion.getWarehousingNum()));


                    //修改仓库表中对应的数据状态
                    List<WmsStockInfo> wmsStockInfoList = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialBatch, pickingRestDbVo.getInputValue()));
                    List<WmsStockInfo> wmsStockInfoArrayList = new ArrayList<>();

                    if (sysDocument == null) {
                        pickingRestDbDto.setMessage("没有查询到相关单据配置");
                        pickingRestDbDto.setCode("500");
                        return pickingRestDbDto;
                    }


                    //修改明细状态为已入库
                    for (WipCompletionDetail entity : wipCompletionDetailList) {
                        entity.setStatus(3);
                        entity.setWarehouseInTime(new Date());
                        entity.setWarehouseInBy(LoginHelper.getUsername());
                        entity.setWarehouseMessage(pickingRestDbVo.getCombinationName());
                        entity.setReceiptQuantity(1L);
                    }
                    wipCompletionDetailMapper.updateBatchById(wipCompletionDetailList);

                    //修改采购打印状态
                    IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialPrinting>().lambda()
                        .eq(IncomingMaterialPrinting::getBatchNo, pickingRestDbVo.getInputValue()));
                    incomingMaterialPrinting.setPrintStatus(4);
                    incomingMaterialPrintingMapper.updateById(incomingMaterialPrinting);

                    //并且校验是否还有待入库的明细
                    List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
                    boolean allTrue = true;
                    for (WipCompletionDetail entity : wipCompletionDetails) {
                        if (entity.getStatus() == 1) {
                            allTrue = false;
                            break;
                        }
                    }
                    //实际量
                    double aDouble = Double.parseDouble(wipCompletion.getActualQuantity());
                    //计划量
                    double plannedWeight = wipCompletion.getPlannedWeight();
                    boolean b = Double.doubleToLongBits(aDouble) >= Double.doubleToLongBits(plannedWeight);
                    if (b) {
                        //修改仓库数据
                        for (WmsStockInfo entity : wmsStockInfoList) {
                            //获取物料类别信息
                            SysApiCategoryDto data = remoteCodingService.getByCode(entity.getWsiMaterialCode()).getData();
                            entity.setWsiMaterialStatus(2);
                            entity.setWsiCategory(String.valueOf(data.getCategoryId()));
                            entity.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                            entity.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
                            entity.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
                            entity.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
                            entity.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
                            entity.setWsiConnectNum(wipCompletion.getMakingOrderNo());
                            entity.setWsiDocNum(wipCompletion.getReceiptNo());
                            entity.setCreateUser(LoginHelper.getUsername());
                            entity.setCreateTime(new Date());
                            entity.setWsiReveiveTime(new Date());
                            entity.setWsiCustomCode(wipCompletion.getCustomerCode());
                            if (data1.getUnitQuantity() != null) {
                                entity.setWsiMinunitNum(Integer.parseInt(data1.getUnitQuantity()));
                            }
                            wmsStockInfoArrayList.add(entity);
                        }
                        wmsStockInfoMapper.updateBatchById(wmsStockInfoArrayList);

                        //判断是否配置了超收
                        if (sysDocument.getDocExcessMark() == 2) {
                            //如果未配置超收，且实际量大于等于计划量，就将单据修改为已完成
                            if (allTrue) {
                                wipCompletion.setStatus(4);
                            } else {
                                wipCompletion.setStatus(2);
                            }
                            pickingRestDbDto.setCode("200");
                            wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getCkId());
                            wipCompletionMapper.updateById(wipCompletion);
                            return pickingRestDbDto;
                        }
//                        if (allTrue) {
//                            wipCompletion.setStatus(3);
//                        } else {
//                            wipCompletion.setStatus(2);
//                        }
                        wipCompletion.setStatus(2);
                        //判断该单据在打印页面是否还存在未入库数据
                        String receiptNo = wipCompletion.getReceiptNo();
                        List<IncomingMaterialPrinting> incomingMaterialPrintings = incomingMaterialPrintingMapper.selectList(new QueryWrapper<IncomingMaterialPrinting>()
                            .lambda().eq(IncomingMaterialPrinting::getReceiptNo, receiptNo));
                        boolean printAll = true;
                        for (IncomingMaterialPrinting entity : incomingMaterialPrintings) {
                            if (entity.getPrintStatus() != 4) {
                                printAll = false;
                                break;
                            }
                        }
                        if (printAll) {
                            if (sysDocument.getDocExcessMark() == 1) {
                                wipCompletion.setStatus(2);
                            } else {
                                wipCompletion.setStatus(3);
                            }
                        } else {
                            wipCompletion.setStatus(2);
                        }
                        pickingRestDbDto.setCode("200");
                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getCkId());
                        wipCompletionMapper.updateById(wipCompletion);
                        return pickingRestDbDto;

                    } else {
//                        if (allTrue) {
//                            wipCompletion.setStatus(3);
//                        } else {
//                            wipCompletion.setStatus(2);
//                        }
                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
                        wipCompletionMapper.updateById(wipCompletion);

                        //修改仓库数据
                        for (WmsStockInfo entity : wmsStockInfoList) {
                            //获取物料类别信息
                            SysApiCategoryDto data = remoteCodingService.getByCode(entity.getWsiMaterialCode()).getData();
                            entity.setWsiMaterialStatus(2);
                            entity.setWsiCategory(String.valueOf(data.getCategoryId()));
                            entity.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                            entity.setWsiWarehourse(String.valueOf(pickingRestDbVo.getCkId()));
                            entity.setWsiReservoirArea(String.valueOf(pickingRestDbVo.getKqId()));
                            entity.setWsiStorageLoaction(String.valueOf(pickingRestDbVo.getKwId()));
                            entity.setWsiConnectNum(wipCompletion.getMakingOrderNo());
                            entity.setWsiDocNum(wipCompletion.getReceiptNo());
                            entity.setWsiWarehourseInfo(pickingRestDbVo.getCombinationName());
                            entity.setWsiReveiveTime(new Date());
                            entity.setCreateUser(LoginHelper.getUsername());
                            entity.setCreateTime(new Date());
                            entity.setWsiCustomCode(wipCompletion.getCustomerCode());
                            if (data1.getUnitQuantity() != null) {
                                entity.setWsiMinunitNum(Integer.parseInt(data1.getUnitQuantity()));
                            }
                            wmsStockInfoArrayList.add(entity);
                        }
                        wmsStockInfoMapper.updateBatchById(wmsStockInfoArrayList);

                        wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getKwId());
                        wipCompletionMapper.updateById(wipCompletion);
                        pickingRestDbDto.setCode("200");
                        return pickingRestDbDto;
                    }
                }
            }


            //库位撤销
            if ("3".equals(pickingRestDbVo.getType())) {
                //查询该条码是否扫描过
                WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDbVo.getInputValue()));
                if (wmsStockInfo == null) {
                    pickingRestDbDto.setMessage("撤销失败，请核实后再录入");
                    pickingRestDbDto.setCode("500");
                    return pickingRestDbDto;
                }
                wmsStockInfo.setWsiMaterialStatus(1);
                wmsStockInfo.setWsiWarehourse("");
                wmsStockInfo.setWsiReservoirArea("");
                wmsStockInfo.setWsiStorageLoaction("");
                wmsStockInfo.setWsiWarehourseInfo("");
                wmsStockInfoMapper.updateById(wmsStockInfo);

                //同时修改完工入库明细状态
                WipCompletionDetail wipCompletionDetail = wipCompletionDetailMapper.selectOne(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getMaterialNumber, pickingRestDbVo.getInputValue()));
                wipCompletionDetail.setStatus(1);
                wipCompletionDetail.setWarehouseMessage("");
                wipCompletionDetail.setWarehouseInBy("");
                wipCompletionDetailMapper.updateById(wipCompletionDetail);

                //完工入库单数量和重量扣减
                BigDecimal subtract = new BigDecimal(wipCompletion.getActualQuantity()).subtract(new BigDecimal(String.valueOf(wipCompletionDetail.getNetWeight())));
                wipCompletion.setActualQuantity(subtract.toString());

                //判断条数修改完工入库状态
                List<WipCompletionDetail> list = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, pickingRestDbVo.getId()));
                boolean isSuccess = true;
                for (WipCompletionDetail entity : list) {
                    if (entity.getStatus() == 1) {
                        isSuccess = false;
                        break;
                    }
                }
                //如果全是待入库状态就修改为待入库
                if (isSuccess) {
                    wipCompletion.setStatus(1);
                    wipCompletion.setWarehousingWarehouse(pickingRestDbVo.getCkId());

                }
                wipCompletionMapper.updateById(wipCompletion);

                //单位
                String unitName = remoteUnitService.getUnitDataByCode(Long.parseLong(wipCompletion.getMaterialUnit())).getData().getUnitName();
                //设置返回信息
                pickingRestDbDto.setMessage("物料号码:" + wipCompletionDetail.getMaterialNumber() + "," + "数量:" + wipCompletionDetail.getReceiptQuantity() + "," + "撤销成功");
                pickingRestDbDto.setQuantityIdentification(wipCompletion.getPlannedWeight() + "/" + wipCompletion.getActualQuantity() + unitName);
//                    pickingRestDbDto.setUnit(publicGoods.getMaterialUnit());
                pickingRestDbDto.setUnitDict(unitName);
                pickingRestDbDto.setWarehouseNumber(String.valueOf(wipCompletion.getWarehousingNum()));
                pickingRestDbDto.setCode("200");
                return pickingRestDbDto;
            }
        } else {
            pickingRestDbDto.setMessage("只能操作待入库和入库中得数据");
            pickingRestDbDto.setCode("500");
            return pickingRestDbDto;
        }
        return pickingRestDbDto;
    }


    /**
     * 完工入库拣货撤销
     *
     * @param pickingRestDbVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pickingCancellation(PickingRestDbVo pickingRestDbVo) {
        //根据其它入库id获取明细数据
        List<WipCompletionDetail> wipCompletionDetailList = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, pickingRestDbVo.getId()));
        //根据批次号查询仓库信息
        List<WmsStockInfo> wmsStockInfoList = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialBatch, wipCompletionDetailList.get(0).getBatchNo()));
        //删除仓储数据
        wmsStockInfoMapper.deleteBatchIds(wmsStockInfoList);
        //删除明细数据
        wipCompletionDetailMapper.deleteBatchIds(wipCompletionDetailList);
        //修改销售退货状态
        WipCompletion wipCompletion = wipCompletionMapper.selectById(pickingRestDbVo.getId());
        wipCompletion.setStatus(1);
        wipCompletion.setWarehousingNum(0);
        wipCompletion.setWarehousingWeight(0);
        wipCompletion.setActualQuantity("0");
        return wipCompletionMapper.updateById(wipCompletion) > 0;
    }


    /**
     * 完工入库拣货完成
     *
     * @param pickingRestDbVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pickingCompleted(PickingRestDbVo pickingRestDbVo) throws Exception {
        WipCompletion wipCompletion = wipCompletionMapper.selectById(pickingRestDbVo.getId());
        //获取单据配置
        Long docConfig = remoteMenuService.getIdByMenuName("完工入库");
        SysDocumentResultDto sysDocument = remoteDocumentService.getInfoConfig(docConfig);

        //配置了超收
        if (sysDocument.getDocExcessMark() == 1) {
            //判断该明细下物料是否都已经入库
            boolean isAll = true;
            List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
            for (WipCompletionDetail entity : wipCompletionDetails) {
                if (entity.getStatus() == 1) {
                    isAll = false;
                    break;
                }
            }
            if (isAll) {
                wipCompletion.setStatus(3);
            }
        }
        return wipCompletionMapper.updateById(wipCompletion) > 0;
    }

    /**
     * 完工入库入库打印
     *
     * @param warehousInspectionVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean wipPrinting(WarehousInspectionVo warehousInspectionVo) throws Exception {
        WipCompletion wipCompletion = wipCompletionMapper.selectById(warehousInspectionVo.getId());
        if (wipCompletion.getStatus() == 1 || wipCompletion.getStatus() == 2) {
            //修改状态为入库中
            wipCompletion.setStatus(2);
            wipCompletion.setMaterialUnit(warehousInspectionVo.getMaterialUnit());
            List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = new ArrayList<>();
            IncomingMaterialPrinting incomingMaterialPrinting = new IncomingMaterialPrinting();
            incomingMaterialPrinting.setDocumentType(wipCompletion.getDocumentType())
                .setDeptId(LoginHelper.getDeptId())
                .setCustomerCode(wipCompletion.getCustomerCode())
                .setReceiptNo(wipCompletion.getReceiptNo())
                .setQuantityBatch(warehousInspectionVo.getNumberCars())
                .setCreateBy(LoginHelper.getUsername())
                .setCreateTime(new Date())
                //唯一标识
                .setOnlyMark(remoteCodeService.splitList("IMP"))
                //生成批次号
                .setBatchNo(batchNoGenerate())
                .setMaterialCode(wipCompletion.getMaterialCode());
//                //如果是免检就保存打印状态为待打印，如果不是就保存待检验
//                if (warehousInspectionVo.getInspectionExemption() == 1) {
//                    incomingMaterialPrinting.setPrintStatus(2);
//                } else {
//                    incomingMaterialPrinting.setPrintStatus(1);
//                }
            incomingMaterialPrinting.setPrintStatus(2);
            incomingMaterialPrintingMapper.insert(incomingMaterialPrinting);
            //存储打印明细数据
            for (int i = 0; i <= warehousInspectionVo.getNumberCars() - 1; i++) {
                IncomingMaterialPrintingDetail incomingMaterialPrintingDetail = new IncomingMaterialPrintingDetail();
                incomingMaterialPrintingDetail.setPrintId(incomingMaterialPrinting.getId())
                    .setBatchNo(incomingMaterialPrinting.getBatchNo())
                    .setReceiptQuantity(1)
                    .setMaterialCategory(wipCompletion.getMaterialCategory())
                    .setMaterialId(wipCompletion.getMaterialId())
                    .setMaterialCode(incomingMaterialPrinting.getMaterialCode())
                    .setMaterialNumber(remoteCodeService.splitList("CGRKDYNUM"))
                    .setMaterialUnit(warehousInspectionVo.getMaterialUnit())
                    .setReceiptId(wipCompletion.getId());
//                    //如果是免检就保存净重和毛重数据
//                    if (warehousInspectionVo.getInspectionExemption() == 1) {
//                        incomingMaterialPrintingDetail.setGrossWeight(entity.getNetWeight() / entity.getNumberCars());
//                        incomingMaterialPrintingDetail.setNetWeight(entity.getNetWeight() / entity.getNumberCars());
//                    } else {
//                        incomingMaterialPrintingDetail.setGrossWeight(entity.getNetWeight() / entity.getNumberCars());
//                    }
                incomingMaterialPrintingDetail.setNetWeight(Double.parseDouble(warehousInspectionVo.getUnitContent()));
                incomingMaterialPrintingDetails.add(incomingMaterialPrintingDetail);
            }
            incomingMaterialPrintingDetailService.batchSave(incomingMaterialPrintingDetails);

            return wipCompletionMapper.updateById(wipCompletion) > 0;
        } else {
            throw new GlobalException("只能操作待入库的数据");
        }
    }

    /**
     * 根据单据号获取完工入库信息（成本调整使用）
     */
    @Override
    public WipCompletionMessageDto getWipCompletionMessage(String documentNumber) {

        return wipCompletionMapper.getWipCompletionMessage(documentNumber);
    }

    /**
     * 修改完工入库信息（成本调整使用）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWipByAdjustment(UpdateWipByAdjustmentVo updateWipByAdjustmentVo) {
        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>()
            .lambda().eq(WipCompletion::getReceiptNo, updateWipByAdjustmentVo.getDocumentNumber()));
        wipCompletion.setUnitPrice(updateWipByAdjustmentVo.getUnitPrice())
            .setWarehousingWarehouse(updateWipByAdjustmentVo.getWarehouse());
        return wipCompletionMapper.updateById(wipCompletion) > 0;
    }

    /**
     * 获取完工入库单中的入库仓库(成本调整使用)
     */
    @Override
    public List<WarehouseInDto> getWarehouseIn() {
        List<WarehouseInDto> warehouseIn = wipCompletionMapper.getWarehouseIn();
        return warehouseIn.stream().filter(entity -> entity.getWareHouseName() != null).collect(Collectors.toList());
    }

    /**
     * 筛选的仓库单据为已完成或已关结的完工入库单(成本调整使用)
     */
    @Override
    public TableDataInfo<WipCompletionDto> screenDocument(WipCompletionVo wipCompletionVo, PageQuery pageQuery) {
        Page<WipCompletionDto> wipCompletionDtoPage = wipCompletionMapper.screenDocument(wipCompletionVo, pageQuery.build());
        List<WipCompletionDto> records = wipCompletionDtoPage.getRecords();
        for (WipCompletionDto entity : records) {
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //单据类型
            entity.setDocumentTypeDict(DocumentTypeEnum.getValue(entity.getDocumentType()));
            //单位
            entity.setUnitDict(remoteUnitService.getUnitDataByCode(Long.parseLong(entity.getMaterialUnit())).getData().getUnitName());
        }
        return TableDataInfo.build(wipCompletionDtoPage);
    }

    /**
     * 根据物料编码获取采购入库及完工入库已完成、已关节状态单据信息
     */
    @Override
    public List<PrintDocumentDto> getPrintDocument(String materialCode) {
        List<PrintDocumentDto> result = new ArrayList<>();
        //查询采购入库单据信息
        List<PrintDocumentDto> purchaseReceiptMessage = purchaseReceiptMapper.getPurchaseReceiptMessage(materialCode);
        if (purchaseReceiptMessage.size() > 0) {
            result.addAll(purchaseReceiptMessage);
        }
        //查询完工入库单据信息
        List<PrintDocumentDto> wipCompletionDocumentMessage = wipCompletionMapper.getWipCompletionDocumentMessage(materialCode);
        if (wipCompletionDocumentMessage.size() > 0) {
            result.addAll(wipCompletionDocumentMessage);
        }
        return result;
    }

    /**
     * 批次号生成
     */
    public String batchNoGenerate() {
        String batchNo = "";
        String prefix = "WG";
        //查询缓存
        Object cacheObject = RedisUtils.getCacheObject(VariousKeyEnum.getValue(1));
        if (cacheObject == null) {
            String num = "001";
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            String format = sf.format(new Date());
            batchNo = prefix + format + num;
            //放入缓存
            RedisUtils.setCacheObject(VariousKeyEnum.getValue(1), num);
            return batchNo;
        }
        String s = cacheObject.toString();
        String s1 = addOne(s);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sf.format(new Date());
        batchNo = format + s1;
        //放入缓存
        RedisUtils.setCacheObject(VariousKeyEnum.getValue(1), s1);
        return batchNo;
    }

    //字符串末尾加1
    public String addOne(String testStr) {
        //根据不是数字的字符拆分字符串
        String[] strs = testStr.split("[^0-9]");
        //取出最后一组数字
        String numStr = strs[strs.length - 1];
        //如果最后一组没有数字(也就是不以数字结尾)，抛NumberFormatException异常
        if (numStr != null && numStr.length() > 0) {
            //取出字符串的长度
            int n = numStr.length();
            //将该数字加一
            int num = Integer.parseInt(numStr) + 1;
            String added = String.valueOf(num);
            n = Math.min(n, added.length());
            //拼接字符串
            return testStr.subSequence(0, testStr.length() - n) + added;
        } else {
            throw new NumberFormatException();
        }
    }

//    /**
//     * 新增完工入库（其他模块调用）
//     *
//     * @param wipOtherAddVo
//     * @return
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public boolean wipOtherAdd(WipOtherAddVo wipOtherAddVo) throws Exception {
//
//        //根据制令单号获取自动生成数据
//        WipCompletionDto autoGenerationData = wipCompletionMapper.getAutoGenerationData(wipOtherAddVo);
//        if (autoGenerationData == null || "4".equals(autoGenerationData.getStatus()) || "5".equals(autoGenerationData.getStatus())) {
//
//            //新增完工入库单
//            WipCompletion wipCompletion1 = new WipCompletion();
//            String authorityCoding = feignService.getAuthorityCoding("warehouse:warehousing:finished:list");
//            if (authorityCoding == null) {
//                throw new GlobalException("该单据未进行配置，无法生成单据编码");
//            }
//            wipCompletion1.setReceiptNo(authorityCoding)
//                .setDeptId(SecurityUtils.getDeptId())
//                .setStatus(1)
//                .setMakingOrderNo(wipOtherAddVo.getMakingOrderNo())
//                .setMaterialCode(wipOtherAddVo.getMaterialCode())
//                .setMaterialCategory(wipOtherAddVo.getMaterialCategory())
//                .setPlannedWeight(wipOtherAddVo.getPlannedWeight())
//                .setWarehousTime(wipOtherAddVo.getWarehousTime())
//                .setDrawerBy(SecurityUtils.getUsername())
//                .setDrawerTime(new Date())
//                .setReceiveType(wipOtherAddVo.getReceiveType())
//                .setDocumentType(DocumentTypeEnum.WIP_COMPLETION.getCode());
//
//            if (wipOtherAddVo.getGenerationMethod() != 2) {
//                wipCompletion1.setGenerationMethod(GenerationMethodEnum.MANUALLY_GENERATED.getCode());
//            } else {
//                wipCompletion1.setGenerationMethod(GenerationMethodEnum.AUTO_GENERATE.getCode());
//            }
//            boolean a = save(wipCompletion1);
//            if (a) {
//                //保存完工入库明细数据
//                WipCompletionDetail wipCompletionDetail = new WipCompletionDetail();
//                wipCompletionDetail.setMaterialCode(wipOtherAddVo.getMaterialCode())
//                    .setMaterialNumber(wipOtherAddVo.getMaterialNumber())
//                    .setStatus(1)
//                    .setGrossWeight(wipOtherAddVo.getGrossWeight())
//                    .setNetWeight(wipOtherAddVo.getNetWeight())
//                    .setReceiptQuantity(1)
//                    .setBatchNo(wipOtherAddVo.getBatchNo())
//                    .setWipCompletionId(wipCompletion1.getId());
//                wipCompletionDetailService.save(wipCompletionDetail);
//            }
//            return a;
//        } else {
//            //将成品称重数据进行保存到明细
//            WipCompletionDetail wipCompletionDetail = new WipCompletionDetail();
//            wipCompletionDetail.setMaterialCode(wipOtherAddVo.getMaterialCode())
//                .setMaterialNumber(wipOtherAddVo.getMaterialNumber())
//                .setStatus(1)
//                .setGrossWeight(wipOtherAddVo.getGrossWeight())
//                .setNetWeight(wipOtherAddVo.getNetWeight())
//                .setReceiptQuantity(1)
//                .setBatchNo(wipOtherAddVo.getBatchNo())
//                .setWipCompletionId(autoGenerationData.getId());
//            return wipCompletionDetailService.save(wipCompletionDetail);
//        }
//    }

//    /**
//     * 完工入库品质回传状态(fegin调用)
//     *
//     * @param updateStatusByQualityVo
//     * @return
//     */
//    @Override
//    public boolean updateStatusByQuality(UpdateStatusByQualityVo updateStatusByQualityVo) {
////        Long menuId = feignService.getIdByMenuName("完工入库");
////        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
////        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
//        //获取完工入库及对应明细信息
//        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>().lambda().eq(WipCompletion::getReceiptNo, updateStatusByQualityVo.getAssociatedNo()));
//
//        List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//        //如果单据状态为待检验
//        if (wipCompletion.getStatus() == 3) {
//            wipCompletion.setStatus(4);
//            for (WipCompletionDetail entity : wipCompletionDetails) {
//                entity.setStatus(3);
//            }
//        }
//        if (wipCompletionDetails.size() > 0) {
//            wipCompletionDetailService.updateBatchById(wipCompletionDetails);
//        }
//
//        return updateById(wipCompletion);
//    }
//
//    /**
//     * 完工入库品质回传状态(品质处理fegin调用)
//     *
//     * @param returnStatusVo
//     * @return
//     */
//    @Override
//    public boolean wipCompletionQualityStatus(ReturnStatusVo returnStatusVo) {
//        String associatedNo = returnStatusVo.getAssociatedNo();
//        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>().lambda().eq(WipCompletion::getReceiptNo, associatedNo));
//        wipCompletion.setStatus(4);
//        updateById(wipCompletion);
//        List<WipCompletionDetail> wipCompletionDetails = wipCompletionDetailMapper.selectList(new QueryWrapper<WipCompletionDetail>().lambda().eq(WipCompletionDetail::getWipCompletionId, wipCompletion.getId()));
//
//        List<String> materialNumberList = new ArrayList<>();
//        for (WipCompletionDetail entity : wipCompletionDetails) {
//            materialNumberList.add(entity.getMaterialNumber());
//            entity.setStatus(3);
//        }
//
//        wipCompletionDetailService.updateBatchById(wipCompletionDetails);
//
//        //修改对应仓库中物料的等级和状态
//        List<WmsStockInfo> wmsStockInfos = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, materialNumberList));
//        for (WmsStockInfo wmsStockInfo : wmsStockInfos) {
//            wmsStockInfo.setWsiMaterialGrade(String.valueOf(returnStatusVo.getJudgmentGrade()));
//            wmsStockInfo.setWsiMaterialStatus(2);
//        }
//
//
//        return wmsStockInfoService.updateBatchById(wmsStockInfos);
//    }
}
